package com.sms.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sms.graduate.dao.SmsUserDao;
import com.sms.graduate.entity.SmsUser;
import com.sms.graduate.service.SmsUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SmsUserServiceImpl extends ServiceImpl<SmsUserDao, SmsUser> implements SmsUserService {

    @Override
    public SmsUser userLogin(String userCode, String password) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsUser.COL_USERCODE,userCode)
                .eq(SmsUser.COL_PASSWORD,password);
        return this.getOne(queryWrapper);
    }

    @Override
    public SmsUser selectUserByCode(String usercode) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsUser.COL_USERCODE,usercode);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<SmsUser> selectStudentByClass(String userClass) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(SmsUser.COL_ROLE,0)
                .eq(SmsUser.COL_USERCLASS,userClass);
        List<SmsUser> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<SmsUser> selectAllStudent() {
        List<SmsUser> list = this.list();
        return list;
    }

    @Override
    public Boolean updateStudentInfo(SmsUser student) {
        UpdateWrapper<SmsUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(SmsUser.COL_USERCODE,student.getUsercode());
        return this.update(student, updateWrapper);
    }

    @Override
    public Boolean addStudentInfo(SmsUser student) {
        return this.save(student);
    }

    @Override
    public String selectTeacher(String courseClass) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsUser.COL_ROLE,2)
                .eq(SmsUser.COL_USERCLASS,courseClass);
        SmsUser one = this.getOne(queryWrapper);
        return one.getUsercode();
    }

    @Override
    public List<SmsUser> queryUsers(SmsUser smsUser) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        // 班级
        if (smsUser.getUserClass()!=null && !"".equals(smsUser.getUserClass())){
            queryWrapper.eq(SmsUser.COL_USERCLASS,smsUser.getUserClass());
        }
        // 专业
        if (smsUser.getUserMajor()!=null && !"".equals(smsUser.getUserMajor())){
            queryWrapper.eq(SmsUser.COL_USERMAJOR,smsUser.getUserMajor());
        }
        // 宿舍
        if (smsUser.getUserHostel()!=null && !"".equals(smsUser.getUserHostel())){
            queryWrapper.eq(SmsUser.COL_USERHOSTEL,smsUser.getUserHostel());
        }
        // 用户类型
        if (smsUser.getRole()!=null && !"".equals(smsUser.getRole())){
            queryWrapper.eq(SmsUser.COL_ROLE,smsUser.getRole());
        }

        List<SmsUser> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<SmsUser> queryTeacher(SmsUser smsUser) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(smsUser.getUsercode())){
            queryWrapper.eq(SmsUser.COL_USERCODE,smsUser.getUsercode());
        }
        if (StringUtils.isNotBlank(smsUser.getUsername())){
            queryWrapper.like(SmsUser.COL_USERNAME,smsUser.getUsername());
        }
        if (StringUtils.isNotBlank(smsUser.getSex())){
            queryWrapper.eq(SmsUser.COL_SEX,smsUser.getSex());
        }
        if (StringUtils.isNotBlank(smsUser.getRole())){
            queryWrapper.like(SmsUser.COL_ROLE,smsUser.getRole());
        }
        if (StringUtils.isNotBlank(smsUser.getRemark())){
            queryWrapper.like(SmsUser.COL_REMARK,smsUser.getRemark());
        }
        queryWrapper.and(a -> a
                .eq(SmsUser.COL_ROLE,"1")
                .or().eq(SmsUser.COL_ROLE,"2")
                );
        List<SmsUser> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<SmsUser> noHostelOfStudent() {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsUser.COL_ROLE,"0")
                .and(a->a
                        .eq(SmsUser.COL_USERHOSTEL,"")
                        .or()
                        .isNull(SmsUser.COL_USERHOSTEL)
                );
        List<SmsUser> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public Boolean updateUserHostel(String userHostel,String usercode) {
        UpdateWrapper<SmsUser> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.set(SmsUser.COL_USERHOSTEL,userHostel)
                .eq(SmsUser.COL_USERCODE,usercode);
        this.update(userUpdateWrapper);
        return true;
    }

    @Override
    public List<SmsUser> queryHostelPeople(String userHostel) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsUser.COL_USERHOSTEL,userHostel)
                .eq(SmsUser.COL_ROLE,"0");
        return this.list(queryWrapper);
    }

    @Override
    public Page<SmsUser> selectClassInfo(SmsUser smsUser) {
        Page<SmsUser> p = new Page<>(smsUser.getPage(),smsUser.getLimit());
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(smsUser.getUserMajor())){
            queryWrapper.eq(SmsUser.COL_USERMAJOR,smsUser.getUserMajor());
        }
        if (StringUtils.isNotBlank(smsUser.getRole())){
            queryWrapper.eq(SmsUser.COL_USERCLASS,smsUser.getUserClass());
        }
        if (StringUtils.isNotBlank(smsUser.getRole())){
            queryWrapper.eq(SmsUser.COL_ROLE,smsUser.getRole());
        }
        if (StringUtils.isNotBlank(smsUser.getSex())) {
            queryWrapper.eq(SmsUser.COL_SEX,smsUser.getSex());
        }
        if (StringUtils.isNotBlank(smsUser.getUsercode())) {
            queryWrapper.eq(SmsUser.COL_USERCODE,smsUser.getUsercode());
        }
        // 查询学生列表时，只查询班主任跟学生
        queryWrapper.and(a->a
                .eq(SmsUser.COL_ROLE,0)
                .or().eq(SmsUser.COL_ROLE,2)
        );
        queryWrapper.orderByDesc(SmsUser.COL_ROLE)
                .orderByAsc(SmsUser.COL_USERCODE);
        Page<SmsUser> page = this.page(p, queryWrapper);
        return page;
    }

    @Override
    public List<SmsUser> queryMajorClass(SmsUser smsUser) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SmsUser.COL_USERMAJOR,SmsUser.COL_USERCLASS,"IFNULL(COUNT(UserCode),0) AS numPeople");
        queryWrapper.groupBy(SmsUser.COL_USERMAJOR,SmsUser.COL_USERCLASS);
        if (StringUtils.isNotBlank(smsUser.getUserMajor())){
            queryWrapper.like(SmsUser.COL_USERMAJOR,smsUser.getUserMajor());
        }
        if (StringUtils.isNotBlank(smsUser.getUserClass())){
            queryWrapper.eq(SmsUser.COL_USERCLASS,smsUser.getUserClass());
        }
        return this.list(queryWrapper);
    }

    @Override
    public boolean updateUserInfo(SmsUser smsUser) {
        UpdateWrapper<SmsUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(SmsUser.COL_PHONE,smsUser.getPhone())
                .set(SmsUser.COL_EMAIL,smsUser.getEmail())
                .eq(SmsUser.COL_USERCODE,smsUser.getUsercode());
        this.update(updateWrapper);
        return true;
    }

    @Override
    public List<String> getTeacherCodeByUserInfo(SmsUser smsUser, String remark) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsUser.COL_USERMAJOR,smsUser.getUserMajor())
                .eq(SmsUser.COL_USERCLASS,smsUser.getUserClass());
        if (StringUtils.isNoneEmpty(remark)){
            queryWrapper.eq(SmsUser.COL_REMARK,remark);
        }
        List<SmsUser> list = this.list(queryWrapper);
        List<String> collect = list.stream().map(a -> a.getUsercode()).collect(Collectors.toList());
        return collect;
    }

    @Override
    public String usercodeByClass(String userMajor, String userClass,String role) {
        QueryWrapper<SmsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SmsUser.COL_USERCLASS,userClass)
                .eq(SmsUser.COL_ROLE,role)
                .eq(SmsUser.COL_USERMAJOR,userMajor);
        List<SmsUser> list = this.list(queryWrapper);
        if (list.size()>0 && list !=null){
            List<String> collect = list.stream().map(SmsUser::getUsercode).collect(Collectors.toList());
            List<String> collect1 = collect.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
            return collect1.get(0);
        }
        return "";
    }

    @Override
    public String usercodeByDate(String year, String mouth) {
        SmsUser smsUser = new SmsUser();
        List<SmsUser> userList = queryTeacher(smsUser);
        if (userList != null && userList.size()>0){
            List<String> collect = userList.stream().map(a -> {
                String substring = a.getUsercode().substring(0, 6);
                return substring;
            }).collect(Collectors.toList());
            String c = year+mouth;
            List<String> collect1 = collect.stream().filter(d -> d.equals(c)).collect(Collectors.toList());
            if (collect1 !=null && collect1.size()>0){
                int size = collect1.size()+1;
                String e =year+mouth+String.valueOf(size);
                if (size>0 && size <10){
                    e = year+mouth+"0"+size;
                }
                return e;
            }
            return year+mouth+"01";
        }
        return year+mouth+"01";
    }



}

